package com.wjb.xietong.server.topic;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.util.ContentReplace;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class TopicResponse extends BaseResponseDataParse {

	private static TopicResponse instance = null;
	private HeaderInfoResponse headerInfo = null;
	private List<TopicInfo> topicInfoList = null;

	private TopicResponse() {

	}

	public static synchronized TopicResponse instance() {
		if (null == instance) {
			instance = new TopicResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setTopicInfoList(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.TOPICS)) {
			JSONArray jsonArray = response.optJSONArray(IDs.TOPICS);
			if (null != jsonArray) {
				topicInfoList = new ArrayList<TopicResponse.TopicInfo>();
				for (int i = 0; i < jsonArray.length(); i++) {
					TopicInfo topicInfo = new TopicInfo();
					JSONObject jsonObj = jsonArray.optJSONObject(i);
					if (jsonObj.has(IDs.REPLY_COUNT)) {
						topicInfo
								.setReplyCount(jsonObj.optInt(IDs.REPLY_COUNT));
					}
					if (jsonObj.has(IDs.STATUS)) {
						if (TopicStatus.DEFAULT.ordinal() == jsonObj
								.optInt(IDs.STATUS)) {
							topicInfo.setStatus(TopicStatus.DEFAULT);
						} else if (TopicStatus.NORMAL.ordinal() == jsonObj
								.optInt(IDs.STATUS)) {
							topicInfo.setStatus(TopicStatus.NORMAL);
						}
					}
					if (jsonObj.has(IDs.READ_COUNT)) {
						topicInfo.setReadCount(jsonObj.optInt(IDs.READ_COUNT));
					}
					if (jsonObj.has(IDs.REPLY_DATE)) {
						topicInfo.setReplyDate(jsonObj
								.optString(IDs.REPLY_DATE));
					}
					if (jsonObj.has(IDs.LIKE)) {
						topicInfo.setLike(jsonObj.optInt(IDs.LIKE));
					}
					if (jsonObj.has(IDs.TYPE)) {
						if (TopicType.DEFAULT.ordinal() == jsonObj
								.optInt(IDs.TYPE)) {
							topicInfo.setType(TopicType.DEFAULT);
						} else if (TopicType.NORMAL.ordinal() == jsonObj
								.optInt(IDs.TYPE)) {
							topicInfo.setType(TopicType.NORMAL);
						}
					}

					if (jsonObj.has(IDs.AVATAR)) {
						topicInfo.setIconUrl(jsonObj.optString(IDs.AVATAR));
					}
					if (jsonObj.has(IDs.ZAN_PEOPLE)) {
						topicInfo.setZanPeople(jsonObj
								.optString(IDs.ZAN_PEOPLE));
					}
					if (jsonObj.has(IDs.CONTENT)) {
						topicInfo.setContent(jsonObj.optString(IDs.CONTENT));
					}
					if (jsonObj.has(IDs.SIGN)) {
						topicInfo.setSign(jsonObj.optString(IDs.SIGN));
					}
					if (jsonObj.has(IDs.ID)) {
						topicInfo.setTopicId(jsonObj.optLong(IDs.ID));
					}
					if (jsonObj.has(IDs.NICK)) {
						topicInfo.setNick(jsonObj.optString(IDs.NICK));
					}
					if (jsonObj.has(IDs.USER_ID)) {
						topicInfo.setUserId(jsonObj.optLong(IDs.USER_ID));
					}
					if (jsonObj.has(IDs.ZAN_FLAG)) {
						topicInfo.setZanFlag(jsonObj.optInt(IDs.ZAN_FLAG));
					}
					if (jsonObj.has(IDs.ATTACHMENT)) {// 附件信息
						JSONArray attachmentArray = jsonObj
								.optJSONArray(IDs.ATTACHMENT);
						if (null != attachmentArray) {
							List<TopicAttachmentInfo> attachmentInfoList = new ArrayList<TopicResponse.TopicAttachmentInfo>();
							for (int j = 0; j < attachmentArray.length(); j++) {
								TopicAttachmentInfo attachmentInfo = new TopicAttachmentInfo();
								JSONObject attachmentJsonObj = attachmentArray
										.optJSONObject(j);
								if (attachmentJsonObj.has(IDs.FILENAME)) {
									attachmentInfo
											.setFilename(attachmentJsonObj
													.optString(IDs.FILENAME));
								}
								if (attachmentJsonObj.has(IDs.FILESIZE)) {
									attachmentInfo
											.setFilesize(attachmentJsonObj
													.optLong(IDs.FILESIZE));
								}
								if (attachmentJsonObj.has(IDs.ID)) {
									attachmentInfo.setId(attachmentJsonObj
											.optLong(IDs.ID));
								}
								if (attachmentJsonObj.has(IDs.IMAGES)) {
									attachmentInfo.setImages(attachmentJsonObj
											.optString(IDs.IMAGES));
								}
								if (attachmentJsonObj.has(IDs.SUFFIX)) {
									attachmentInfo.setSuffix(attachmentJsonObj
											.optString(IDs.SUFFIX));
								}
								attachmentInfoList.add(attachmentInfo);
							}
							topicInfo.setAttachmentInfoList(attachmentInfoList);
						}
					}

					if (jsonObj.has(IDs.REPLY_LIST)) {// 回复信息
						JSONArray replyArray = jsonObj
								.optJSONArray(IDs.REPLY_LIST);
						if (null != replyArray) {
							List<TopicReplyInfo> replyInfoList = new ArrayList<TopicResponse.TopicReplyInfo>();
							for (int k = 0; k < replyArray.length(); k++) {
								TopicReplyInfo replyInfo = new TopicReplyInfo();
								JSONObject replyJsonObj = replyArray
										.optJSONObject(k);
								if (replyJsonObj.has(IDs.CONTENT)) {
									replyInfo.setContent(replyJsonObj
											.optString(IDs.CONTENT));
								}
								if (replyJsonObj.has(IDs.AVATAR)) {
									replyInfo.setIconUrl(replyJsonObj
											.optString(IDs.AVATAR));
								}
								if (replyJsonObj.has(IDs.NICK)) {
									replyInfo.setNick(replyJsonObj
											.optString(IDs.NICK));
								}
								if (replyJsonObj.has(IDs.REPLY_DATE)) {
									replyInfo.setReplyDate(replyJsonObj
											.optString(IDs.REPLY_DATE));
								}
								if (replyJsonObj.has(IDs.ID)) {
									replyInfo.setReplyId(replyJsonObj
											.optLong(IDs.ID));
								}
								if (replyJsonObj.has(IDs.SIGN)) {
									replyInfo.setSign(replyJsonObj
											.optString(IDs.SIGN));
								}
								if (replyJsonObj.has(IDs.USER_ID)) {
									replyInfo.setUserId(replyJsonObj
											.optLong(IDs.USER_ID));
								}
								replyInfoList.add(replyInfo);
							}
							topicInfo.setTopicReplyInfoList(replyInfoList);
						}
					}
					topicInfoList.add(topicInfo);
				}
				setTopicInfoList(topicInfoList);
			}
			return true;
		}
		return false;
	}

	/**
	 * 工作圈详情
	 */
	public static class TopicInfo {
		private int replyCount = 0;// 回复数
		private TopicStatus status = TopicStatus.DEFAULT;
		private int readCount = 0;// 访问数
		private String replyDate = null;// 发表时间 例：前天13:31:52
		private int like = 0;
		private TopicType type = TopicType.DEFAULT;
		private String iconUrl = null;// icon url
		private String zanPeople = null;
		private String content = null;// 内容
		private String sign = null;// 签名
		private long topicId = 0l;// 工作圈id
		private String nick = null;// 昵称
		private long userId = 0l; // 用户id
		private int zanFlag = -1;// 0：未赞 1：已赞
		private List<TopicAttachmentInfo> attachmentInfoList = null;// 附件信息
		private List<TopicReplyInfo> topicReplyInfoList = null;// 回复信息

		public int getReplyCount() {
			return replyCount;
		}

		public void setReplyCount(int replyCount) {
			this.replyCount = replyCount;
		}

		public TopicStatus getStatus() {
			return status;
		}

		public void setStatus(TopicStatus status) {
			this.status = status;
		}

		public int getReadCount() {
			return readCount;
		}

		public void setReadCount(int readCount) {
			this.readCount = readCount;
		}

		public String getReplyDate() {
			return replyDate;
		}

		public void setReplyDate(String replyDate) {
			this.replyDate = replyDate;
		}

		public int getLike() {
			return like;
		}

		public void setLike(int like) {
			this.like = like;
		}

		public TopicType getType() {
			return type;
		}

		public void setType(TopicType type) {
			this.type = type;
		}

		public String getIconUrl() {
			return iconUrl;
		}

		public void setIconUrl(String iconUrl) {
			this.iconUrl = iconUrl;
		}

		public String getZanPeople() {
			return zanPeople;
		}

		public void setZanPeople(String zanPeople) {
			this.zanPeople = zanPeople;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = ContentReplace.replaceContent(content);
		}

		public String getSign() {
			return sign;
		}

		public void setSign(String sign) {
			this.sign = sign;
		}

		public long getTopicId() {
			return topicId;
		}

		public void setTopicId(long topicId) {
			this.topicId = topicId;
		}

		public String getNick() {
			return nick;
		}

		public void setNick(String nick) {
			this.nick = nick;
		}

		public long getUserId() {
			return userId;
		}

		public void setUserId(long userId) {
			this.userId = userId;
		}

		public int getZanFlag() {
			return zanFlag;
		}

		public void setZanFlag(int zanFlag) {
			this.zanFlag = zanFlag;
		}

		public List<TopicAttachmentInfo> getAttachmentInfoList() {
			return attachmentInfoList;
		}

		public void setAttachmentInfoList(
				List<TopicAttachmentInfo> attachmentInfoList) {
			this.attachmentInfoList = attachmentInfoList;
		}

		public List<TopicReplyInfo> getTopicReplyInfoList() {
			return topicReplyInfoList;
		}

		public void setTopicReplyInfoList(
				List<TopicReplyInfo> topicReplyInfoList) {
			this.topicReplyInfoList = topicReplyInfoList;
		}

	}

	public static enum TopicStatus {
		DEFAULT, NORMAL
	}

	public static enum TopicLikeStatus {
		DEFAULT, NORMAL
	}

	public static enum TopicType {
		DEFAULT, NORMAL
	}

	public static class TopicReplyInfo {
		private String sign = null;// 签名
		private String content = null;// 回复内容
		private long replyId = 0l;// 评论id
		private String replyDate = null;// 回复时间 例：前天 11:20:08
		private String iconUrl = null;// icon url
		private String nick = null;// 昵称
		private long userId = 0l;// 用户id

		public String getSign() {
			return sign;
		}

		public void setSign(String sign) {
			this.sign = sign;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = ContentReplace.replaceContent(content);
		}

		public long getReplyId() {
			return replyId;
		}

		public void setReplyId(long replyId) {
			this.replyId = replyId;
		}

		public String getReplyDate() {
			return replyDate;
		}

		public void setReplyDate(String replyDate) {
			this.replyDate = replyDate;
		}

		public String getIconUrl() {
			return iconUrl;
		}

		public void setIconUrl(String iconUrl) {
			this.iconUrl = iconUrl;
		}

		public String getNick() {
			return nick;
		}

		public void setNick(String nick) {
			this.nick = nick;
		}

		public long getUserId() {
			return userId;
		}

		public void setUserId(long userId) {
			this.userId = userId;
		}

	}

	/**
	 * 附件内容
	 * 
	 * @see 如果是图片则只有id和images字段有内容
	 */
	public static class TopicAttachmentInfo {
		private long id = 0l;
		private String suffix = null;// 文件类型
		private String filename = null;// 文件名
		private String images = null;// 图片地址
		private long filesize = 0l;// 文件大小

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getSuffix() {
			return suffix;
		}

		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}

		public String getImages() {
			return images;
		}

		public void setImages(String images) {
			this.images = images;
		}

		public long getFilesize() {
			return filesize;
		}

		public void setFilesize(long filesize) {
			this.filesize = filesize;
		}
	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<TopicInfo> getTopicInfoList() {
		return topicInfoList;
	}

	public void setTopicInfoList(List<TopicInfo> topicInfoList) {
		this.topicInfoList = topicInfoList;
	}

}
